/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JFreeChartExtensions.chartextensions.dualrangeaxis;

import JFreeChartExtensions.Collections.XYReggresionSeriesCollection;
import JFreeChartExtensions.ExtendableChartPanel;
import JFreeChartExtensions.containers.InternalFrameWithChart.InternalFrameWithChart;
import JFreeChartExtensions.chartextensions.AbstractStorableChartPanelExtension;
import JFreeChartExtensions.storable.StorableExtenstionObject;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TreeMap;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.axis.LogarithmicAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;

/**
 *
 * @author Nobody
 */
public class DualRangeAxisExtension extends AbstractStorableChartPanelExtension {

    @Override
    public void loadStoredExtension(StorableExtenstionObject extenstionObject) {
        Object[] objects = extenstionObject.getSerializedObjects();
        int i = -1;
        for (Object object : objects) {
            chartPanel.mapDatasetToRangeAxisAndNotifyListeners(++i, (Integer) object);
        }

    }

    @Override
    public StorableExtenstionObject storeExtension() {

        TreeMap<Integer, XYReggresionSeriesCollection> collection = chartPanel.getDatasetCollection().getDataSeriesCollections();
        Object[] objects = new Object[collection.size()];
        for (Integer i : collection.keySet()) {
            XYPlot plot = chartPanel.getXYPlot();
            ValueAxis valueAxis = plot.getRangeAxisForDataset(i);
            objects[i] = plot.getRangeAxisIndex(valueAxis);
        }
        return new StorableExtenstionObject(objects, name);
    }
    private JMenuItem menuItem;
    private JCheckBoxMenuItem checkBoxMenuItem;

    public DualRangeAxisExtension(ExtendableChartPanel panel) {
        super(panel);
        name = "DualRangeAxisExtension";
    }

    private void addSecondAxis() {
        if (this.chartPanel.getXYPlot().getRangeAxis() instanceof NumberAxis) {
            this.chartPanel.setRangeAxisAndNotifyListeners(1, new NumberAxis("y"));
        } else {
            this.chartPanel.setRangeAxisAndNotifyListeners(1, new LogarithmicAxis("y"));
        }

    }

    @Override
    public void applyExtension(ChartPanel chartPanel) {
        this.chartPanel = (ExtendableChartPanel) chartPanel;
        menuItem = new JMenuItem("Change second axis name");
        menuItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String name = JOptionPane.showInputDialog(DualRangeAxisExtension.this.chartPanel,
                        "Change chart Title",
                        DualRangeAxisExtension.this.chartPanel.getXYPlot().getRangeAxis(1).getLabel());
                ValueAxis rangeAxis = DualRangeAxisExtension.this.chartPanel.getXYPlot().getRangeAxis(1);
                rangeAxis.setLabel(name);
                DualRangeAxisExtension.this.chartPanel.setRangeAxisAndNotifyListeners(1, rangeAxis);
                DualRangeAxisExtension.this.chartPanel.updateChart();
            }
        });

        checkBoxMenuItem = new JCheckBoxMenuItem("Enable second axis");
        checkBoxMenuItem.setSelected(false);
        checkBoxMenuItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (!checkBoxMenuItem.isSelected()) {
                    DualRangeAxisExtension.this.chartPanel.setRangeAxisAndNotifyListeners(1, null);
                    menuItem.setEnabled(false);
                } else {
                    addSecondAxis();
                    menuItem.setEnabled(true);

                }
            }
        });



        this.chartPanel.getPopupMenu().addSeparator();
        this.chartPanel.getPopupMenu().add(menuItem);
        this.chartPanel.getPopupMenu().add(checkBoxMenuItem);
        this.chartPanel.getPopupMenu().addSeparator();

    }

    @Override
    public void removeExtension() {
        chartPanel.getXYPlot().setRangeAxis(1, null);
        this.chartPanel.getPopupMenu().remove(menuItem);

    }
}
